package com.graduation.manage.service.impl;

import java.util.List;
import com.graduation.common.utils.DateUtils;
import com.graduation.manage.domain.Task;
import com.graduation.manage.domain.TbUser;
import com.graduation.manage.domain.Work;
import com.graduation.manage.domain.dto.WorkDto;
import com.graduation.manage.service.ITaskService;
import com.graduation.manage.service.ITbUserService;
import com.graduation.manage.service.IWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.graduation.manage.mapper.StepMapper;
import com.graduation.manage.domain.Step;
import com.graduation.manage.service.IStepService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审核步骤Service业务层处理
 * 
 * @author zxs
 * @date 2024-11-16
 */
@Service
public class StepServiceImpl implements IStepService 
{
    @Autowired
    private StepMapper stepMapper;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private IWorkService workService;

    @Autowired
    private ITbUserService userService;

    /**
     * 查询审核步骤
     * 
     * @param stepId 审核步骤主键
     * @return 审核步骤
     */
    @Override
    public Step selectStepByStepId(Long stepId)
    {
        return stepMapper.selectStepByStepId(stepId);
    }

    /**
     * 查询审核步骤列表
     * 
     * @param step 审核步骤
     * @return 审核步骤
     */
    @Override
    public List<Step> selectStepList(Step step)
    {
        return stepMapper.selectStepList(step);
    }

    /**
     * 新增审核步骤
     * 
     * @param step 审核步骤
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStep(WorkDto workDto)
    {
        Task task = workDto.getTask();
        if(task.getStatus() != 2) {
            return -1;
        }
        // 1. 添加监管人员审核步骤
        Step step = workDto.getStep();
        step.setOperateName("监管人员");
        step.setStepName("监管人员审核");
        step.setCreateTime(DateUtils.getNowDate());
        step.setUpdateTime(DateUtils.getNowDate());
        step.setStepDesc("已核实，情况属实，审核通过");
        stepMapper.insertStep(step);
        // 2. 更新申请状态
        if(step.getStatus() == 1) {
            task.setStatus(4L);
        } else {
            task.setStatus(3L);
        }
        taskService.updateTask(task);
        // 3. 添加结束步骤
        Step step1 = new Step();
        step1.setStepName("结束");
        step1.setTaskId(task.getTaskId());
        stepMapper.insertStep(step1);
        // 4. 添加毕业设计记录
        Work work = new Work();
        work.setWorkName(task.getTaskName());
        TbUser stu = userService.selectTbUserByUserId(task.getStuId());
        work.setStuId(stu.getUserId());
        work.setStuName(stu.getUserName());
        work.setTchId(stu.getTchId());
        work.setTchName(stu.getTchName());
        work.setWorkDesc(task.getTaskDesc());
        work.setWorkReport(task.getTaskReport());
        work.setCreateTime(DateUtils.getNowDate());
        work.setUpdateTime(DateUtils.getNowDate());
        return workService.insertWork(work);
    }

    /**
     * 修改审核步骤
     * 
     * @param step 审核步骤
     * @return 结果
     */
    @Override
    public int updateStep(Step step)
    {
        step.setUpdateTime(DateUtils.getNowDate());
        return stepMapper.updateStep(step);
    }

    /**
     * 批量删除审核步骤
     * 
     * @param stepIds 需要删除的审核步骤主键
     * @return 结果
     */
    @Override
    public int deleteStepByStepIds(Long[] stepIds)
    {
        return stepMapper.deleteStepByStepIds(stepIds);
    }

    /**
     * 删除审核步骤信息
     * 
     * @param stepId 审核步骤主键
     * @return 结果
     */
    @Override
    public int deleteStepByStepId(Long stepId)
    {
        return stepMapper.deleteStepByStepId(stepId);
    }
}
